<?php
class AdminTransactionOut extends CActiveRecord{
    public static function model($className = __CLASS__) {
        return parent::model ( $className );
    }

    // goi den bang can ket noi   
    public function tableName() {                  
        return 'transaction_out';
    }

    public function getAllRowInDate($fromDate,$toDate,$type,$key_word){
        $rows = array();
        if($type > 0){
            $str = " AND type = $type";
        }
        if($key_word != ""){
            $str = " AND user_name LIKE '%$key_word%'";     
        }
        $sql = "SELECT id, user_id, user_name, tk_chinh as tru_tien, ticket_id, bill_id, doithuong_id, type, content, transaction_date as ngay FROM transaction_out
        WHERE transaction_date BETWEEN '$fromDate' AND '$toDate'".$str." ORDER BY transaction_date DESC";

        $connect = Yii::app()->db;
        $command = $connect->createCommand ($sql); 
        $rows = $command->queryAll();
        return $rows;
    }
    
    public function countRowInDate($fromDate,$toDate,$type,$key_word){
        $rows = array();
        if($type > 0){
            $str = " AND type = $type";
        }
        if($key_word != ""){
            $str = " AND user_name LIKE '%$key_word%'";     
        }
        $sql = "SELECT count(*) AS count, SUM(tk_chinh) AS tru_tien FROM transaction_out
        WHERE transaction_date BETWEEN '$fromDate' AND '$toDate'".$str." ORDER BY transaction_date DESC";

        $connect = Yii::app()->db;
        $command = $connect->createCommand ($sql); 
        $rows = $command->queryRow();
        return $rows;
    }
}
